'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买挑战次数
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var dataApi = require('../../../../libs/dataApi.js');
var vipConf = require('../../../../config/vipConf.js');
var expeditionConf = require('../../../../config/expeditionConf.js');
var atkData = require('../../../../libs/war/atkData');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    var atk_info;
    var def_info;
    var expedition_model;
    var record_id = 0;
    var monster_id = 0;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['expedition', 'role', 'accrued'], cb);
            },
            function (cb) {
                expedition_model = player_manager.expedition;
                var role_model = player_manager.role;
                var monster_open_lvl = constantUtil.value('monster_open_lvl');
                if (role_model.lv < monster_open_lvl) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                var next_pass_id = expeditionConf.getNextMonsterId(expedition_model.pass_id);
                if (next_pass_id === 99999) {
                    return next(null, {
                        code: Code.EXPEDITION_ALREADY_PASS_ALL
                    });
                }
                monster_id = next_pass_id;
                var expedition_conf = dataApi.monster_config.findById(monster_id);
                if (!expedition_conf) {
                    // 副本ID无效
                    return next(null, {
                        code: Code.EXPEDITION_INVALID_MONSTER_ID
                    });
                }

                if (expedition_model.pass_id !== expedition_conf.need_id) {
                    // 未通关前置副本
                    return next(null, {
                        code: Code.EXPEDITION_NEED_PRE_PASS
                    });
                }

                if (expedition_model.box_id !== 0) {
                    // 宝箱未开启
                    return next(null, {
                        code: Code.EXPEDITION_BOX_NOT_OPENED
                    });
                }

                _getAtkInfo(role_id, cb);
            },
            function (info, cb) {
                atk_info = info;
                var rival_idx = expeditionConf.getMonsterIndexById(monster_id);
                def_info = expedition_model.rival_list[rival_idx].toJSON();
                var def_soldier_list = [];
                _.forEach(def_info.soldier_list, function (item) {
                    if (item.attr.curr_hp > 0) {
                        def_soldier_list.push(item);
                    } else {
                        def_soldier_list.push(null);
                    }
                });
                def_info.soldier_list = def_soldier_list;

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    record_id: record_id,
                    atk: atk_info,
                    def: def_info
                }
            });
        }
    );
};

var _getAtkInfo = function (role_id, cb) {
    atkData.getDataForExpedition(role_id, cb);
};
